package com.cx.bank.factory;

import com.cx.bank.dao.BankDaoImpl;
import com.cx.bank.manager.IManager;
import com.cx.bank.manager.ManagerImpl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

/**
 * @author jxau_scf
 * @date 2021/7/17 - 9:59
 */
public class UserDaoFactory {
    private static UserDaoFactory userDaoFactory;
    private ManagerImpl manager;

    public ManagerImpl getManager() {
        return manager;
    }

    public void setManager(ManagerImpl manager) {
        this.manager = manager;
    }

    //私有构造函数初始化
    private UserDaoFactory() throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException {
        manager = ManagerImpl.getInstance();
        File file = new File("src/main/resources/classInfo.properties");
        if (!file.exists()){
            System.out.println("classInfo.properties文件不存在");
        }
        Properties properties = new Properties();
        FileInputStream fis = new FileInputStream(file);
        properties.load(fis);
        String className = properties.getProperty("className");
        Class<?> clazz = Class.forName(className);
        BankDaoImpl bankDao = (BankDaoImpl)clazz.newInstance();
        manager.setBankDao(bankDao);
    }



    public static synchronized UserDaoFactory getInstance() throws ClassNotFoundException, InstantiationException, IllegalAccessException, IOException {
        if (null != userDaoFactory){
            return userDaoFactory;
        }
        return new UserDaoFactory();
    }
}
